<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldBox col_100" style="display:none;">
            <div class="field">
                <input name="cstId" id="cstId" type="text" class="ui-textbox" value="${cstId}">
                <input name="billWayCd" id="billWayCd" type="text" class="ui-textbox" value="${flag}">
            </div>
        </div>
        <div class="fieldset1 clearFix">
            <div class="fieldBox">
                <div class="label"></div>
                <div class="field">
                    <c:if test="${'1'==flag}">
                        <span id="cst_invoice_start_flow_btn" onclick="applyElecInv();" class="sq-button sq-button-round">开电子发票</span>
                    </c:if>
                    <c:if test="${'2'==flag}">
                        <span id="cst_invoice_add_btn" onclick="save();" class="sq-button sq-button-round">保存</span>
                        <span id="cst_invoice_start_flow_btn" class="sq-button sq-button-round">发起申请</span>
                    </c:if>
                </div>
            </div>
            <div class="fieldBox">
                <div class="label"></div>
                <div class="field">
                    <input name="cntNo" id="cntNo" data-value="" placeholder="请选择合同号" ltype="combobox">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label"></div>
                <div class="field">
                    <input name="feeTypList" id="feeTypList" data-value="" placeholder="请选择费用项" ltype="combobox">
                </div>
            </div>
        </div>
    </form>
</div>
<div id="invoiceAddGrid" class="sq-grid"></div>
<script>
    //var grid;
    var cntNoArr = ${cntNosKV};
    $(function () {
        $("#searchBar").ligerForm();

        initGrid();

        $("#cntNo").ligerComboBox({
            valueField: 'key',
            textField: 'value',
            data: cntNoArr,
            width: 200,
            onSelected: function (value) {
                // 根据合同号获取可开费用项
                var params = liger.get("searchBar").getData();
                params.prjId = liger.get("cntNo").getValue();
                params.cntNo = liger.get("cntNo").getText()
                grid.set({
                    url: "<%=path%>/invoice/prjbillapply/initApplyList.do",
                    parms: params,
                    where: f_getWhere,
                    onSuccess: function (data, grid) {
                        grid.loadData(data);
                        liger.get("feeTypList").setValue('');
                        $("#prjId").val(liger.get("cntNo").getValue());
                        $("#lsCntNo").val(liger.get("cntNo").getText());
                    }
                });
            }
        });
        if (cntNoArr.length > 0) {
            // 默认选第一个
            liger.get("cntNo").selectValue(liger.get("cntNo").data[0].key);
        }

        $("#feeTypList").ligerComboBox({
            valueField: 'key',
            textField: 'value',
            width: 150,
            data: ${feeTyps},
            onChangeValue: function () {
                f_search();
            }
        });
    });

    //初始化列表
    function initGrid() {
        grid = $("#invoiceAddGrid").ligerGrid({
            usePager: false,
            method: 'get',
            columns: [{
                display: '业务模式', name: 'bizTypCdNm', width: 150, align: 'center'
            }, {
                display: '支付表号', name: 'paySchNo', width: 180, align: 'left',
                render: function (rowdata, rowindex, value, column) {
                    var h = '';
                    h = '<a href="javascript:detailBtn(';
                    h += "'" + rowdata.id + "',";
                    h += "'" + rowdata.paySchId + "',";
                    h += ')">' + value + '</a>'
                    return h;
                },
            }, {
                display: '费用项', name: 'feeTypCdNm', width: 150, align: 'center',
                render: function (rowdata, rowindex, value) {
                    if (rowdata.payPd > 0) {
                        return "第" + rowdata.payPd + "期" + value;
                    }
                    return value;
                }
            }, {
                display: '资金流向', name: 'capFlowNm', width: 80, align: 'center'
            }, {
                display: '应收金额(元)', name: 'feeAmt', width: 150, align: 'right',
                render: function (rowdata, index, value) {
                    return '&yen;' + FormatMoney(rowdata.feeAmt / 100);
                }
            }, {
                display: '实收金额(元)', name: 'vfAmt', width: 100, align: 'right',
                render: function (rowdata, index, value) {
                    return '&yen;' + FormatMoney(rowdata.vfAmt / 100);
                },
                totalSummary: {
                    render: function (rowdata, column, cell) {
                        if (rowdata.sum) {
                            return '<div style="color:red">合计:' + '&yen;' + FormatMoney(rowdata.sum / 100) + '</div>';
                        } else {
                            return '<div style="color:red">合计:' + '&yen;0.00</div>';
                        }
                    },
                    align: 'left'
                },
            }, {
                display: '开票类型', name: 'billTypCdNm', width: 180, align: 'center',
            }, {
                display: '票据状态', name: 'billStsCdNm', width: 180, align: 'center',
            }],
            width: '100%',
            height: '100%',
            heightDiff: -2,
            root: 'rows',
            record: 'total',
            rownumbers: true,
            enabledEdit: true,
            checkbox: true,
            fixedCellHeight: false,
            usePagerOnLessPagesize: false,

            /*onAfterShowData: function (currentData) {
                SetIframe_height();
                $(".liger_tip").ligerTip();
                var lsCntNo = currentData.lsCntNo
            }*/
        });
        grid.setHeight(100);
        grid.options.isSingleCheck = false;
    }

    function f_search() {
        grid.loadData(f_getWhere());
    }

    function f_getWhere() {
        if (!grid) return null;
        var key = liger.get("feeTypList").getValue();
        var clause = function (rowdata, rowindex) {
            if (key) {
                return rowdata.feeTypCd == (key);
            } else {
                return true;
            }
        };
        return clause;
    }

    // 查看支付表详情
    function detailBtn(id, paySchId) {
        var editFlag = true;
        Add_navTab('tab_prj_quat_prps' + id, '查看支付表', '<%=path%>/project/prjprdpayschm/toPrjPrdPaySchMDetailPageBySchId.do?paySchId=' + paySchId);
    }
</script>
